<template>
  <div>
    <el-dialog :title="title" :close-on-click-modal="false" v-dialogDrag :visible.sync="visible">
      <el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method === 'view' ? 'readonly' : ''"
        :disabled="method === 'view'" label-width="120px">
        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="首字母" prop="initial" :rules="[]">
              <el-input v-model="inputForm.initial" placeholder="请填写首字母"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="品牌名称" prop="brandName" :rules="[]">
              <el-input v-model="inputForm.brandName" placeholder="请填写品牌名称"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="车辆类型" prop="vehicleClassification" :rules="[]">
              <el-select v-model="vehicleClassification" placeholder="请选择车辆类型" multiple style="width: 100%">
                  <el-option v-for="item in $dictUtils.getDictList('vehicle_type')" :key="item.value"
                    :label="item.label" :value="item.value">
                  </el-option>
                </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="品牌图片" prop="brandImage" :rules="[]">
              <el-upload ref="brandImage" v-if="visible" list-type="picture-card"
                :action="`${this.$http.BASE_URL}/sys/file/webupload/upload?uploadPath=/vvehiclebrand/vVehicleBrand`"
                :headers="{ token: $cookie.get('token') }" :on-preview="
                  (file, fileList) => {
                    showImg(file.url);
                  }
                " :on-success="
                  (response, file, fileList) => {
                    inputForm.brandImage = fileList
                      .map(
                        (item) =>
                          (item.response && item.response.url) || item.url
                      )
                      .join('|');
                  }
                " :on-remove="
                  (file, fileList) => {
                    $http
                      .post(
                        `/sys/file/webupload/deleteByUrl?url=${
                          (file.response && file.response.url) || file.url
                        }`
                      )
                      .then(({ data }) => {
                        $message.success(data.msg);
                      });
                    inputForm.brandImage = fileList
                      .map((item) => item.url)
                      .join('|');
                  }
                " :before-remove="
                  (file, fileList) => {
                    return $confirm(`确定移除 ${file.name}？`);
                  }
                " multiple :limit="5" :on-exceed="
                  (files, fileList) => {
                    $message.warning(
                      `当前限制选择 5 个文件，本次选择了 ${
                        files.length
                      } 个文件，共选择了 ${
                        files.length + fileList.length
                      } 个文件`
                    );
                  }
                " :file-list="brandImageArra">
                <i class="el-icon-plus"></i>
              </el-upload>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="车辆品牌排序" prop="brandSort" :rules="[]">
              <el-input v-model="inputForm.brandSort" placeholder="请填写车辆品牌排序"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注信息" prop="remarks" :rules="[]">
              <el-input type="textarea" v-model="inputForm.remarks" placeholder="请填写备注信息"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false">关闭</el-button>
        <el-button type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        title: "",
        method: "",
        visible: false,
        loading: false,
        brandImageArra: [],
        inputForm: {
          id: "",
          initial: "",
          brandName: "",
          brandImage: "",
          brandSort: "",
          remarks: "",
          vehicleClassification:""
        },
        vehicleClassification:[]
      };
    },
    components: {},
    methods: {
      showImg(url) {
        this.$hevueImgPreview(url);
      },
      init(method, id) {
        this.method = method;
        this.inputForm.id = id;
        if (method === "add") {
          this.title = `新建车辆品牌管理`;
        } else if (method === "edit") {
          this.title = "修改车辆品牌管理";
        } else if (method === "view") {
          this.title = "查看车辆品牌管理";
        }
        this.brandImageArra = [];
        this.visible = true;
        this.loading = false;
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields();
          if (method === "edit" || method === "view") {
            // 修改或者查看
            this.loading = true;
            this.$http({
              url: `/vvehiclebrand/vVehicleBrand/queryById?id=${this.inputForm.id}`,
              method: "get",
            }).then(({ data }) => {
              this.inputForm = this.recover(this.inputForm, data.vVehicleBrand);
              this.vehicleClassification = this.inputForm.vehicleClassification.split(",");
              console.log(this.vehicleClassification);
              this.inputForm.brandImage.split("|").forEach((item) => {
                if (item.trim().length > 0) {
                  this.brandImageArra.push({
                    name: decodeURIComponent(
                      item.substring(item.lastIndexOf("/") + 1)
                    ),
                    url: item,
                  });
                }
              });
              this.loading = false;
            });
          }
        });
      },
      // 表单提交
      doSubmit() {
        console.log(this.vehicleClassification.join(','));
        this.inputForm.vehicleClassification = this.vehicleClassification.join(',');
        this.$refs["inputForm"].validate((valid) => {
          if (valid) {
            this.loading = true;
            this.$http({
              url: `/vvehiclebrand/vVehicleBrand/save`,
              method: "post",
              data: this.inputForm,
            }).then(({ data }) => {
              this.loading = false;
              if (data && data.success) {
                this.visible = false;
                this.$message.success(data.msg);
                this.$emit("refreshDataList");
              }
            });
          }
        });
      },
    },
  };
</script>